package network

import (
	"context"
	//	"encoding/json"

	. "gitee.com/liukexing/isrn/api"
)

/*
USAGE
  ipfs bootstrap - Show or edit the list of bootstrap peers.

SYNOPSIS
  ipfs bootstrap

DESCRIPTION

  Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.

  SECURITY WARNING:

  The bootstrap command manipulates the "bootstrap list", which contains
  the addresses of bootstrap nodes. These are the *trusted peers* from
  which to learn about other peers in the network. Only edit this list
  if you understand the risks of adding or removing nodes from this list.

SUBCOMMANDS
  ipfs bootstrap add [<peer>]... - Add peers to the bootstrap list.
  ipfs bootstrap list            - Show peers in the bootstrap list.
  ipfs bootstrap rm [<peer>]...  - Remove peers from the bootstrap list.

  Use 'ipfs bootstrap <subcmd> --help' for more information about each command.
*/
type BootStrapCommand struct {
	Shell
}

type BootStrapOutput struct {
	Peers []string
}

/*
USAGE
  ipfs bootstrap list - Show peers in the bootstrap list.

SYNOPSIS
  ipfs bootstrap list

DESCRIPTION

  Peers are output in the format '<multiaddr>/<peerID>'.
*/
func (b *BootStrapCommand) List() (*BootStrapOutput, error) {
	cmdargs := []string{}

	out := &BootStrapOutput{}
	err := b.Request("bootstrap/list", cmdargs...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	return out, nil

	//	req := b.NewRequest(context.Background(), "bootstrap/list", cmdargs...)

	//	resp, err := req.Send(b.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}
	//	defer resp.Close()

	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	out := &BootStrapOutput{}
	//	err = json.NewDecoder(resp.Output).Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}

	//	return out, nil
}

/*
USAGE
  ipfs bootstrap add [<peer>]... - Add peers to the bootstrap list.

SYNOPSIS
  ipfs bootstrap add [--default] [--] [<peer>...]

ARGUMENTS

  [<peer>]... - A peer to add to the bootstrap list (in the format '<multiaddr>/<peerID>')

OPTIONS

  --default bool - Add default bootstrap nodes. (Deprecated, use 'default' subcommand instead).

DESCRIPTION

  Outputs a list of peers that were added (that weren't already
  in the bootstrap list).

  SECURITY WARNING:

  The bootstrap command manipulates the "bootstrap list", which contains
  the addresses of bootstrap nodes. These are the *trusted peers* from
  which to learn about other peers in the network. Only edit this list
  if you understand the risks of adding or removing nodes from this list.

SUBCOMMANDS
  ipfs bootstrap add default - Add default peers to the bootstrap list.

  Use 'ipfs bootstrap add <subcmd> --help' for more information about each command.
*/

func (b *BootStrapCommand) Add(peer string) (*BootStrapOutput, error) {
	cmdargs := []string{peer}

	out := &BootStrapOutput{}
	err := b.Request("bootstrap/add", cmdargs...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	return out, nil
	//	req := b.NewRequest(context.Background(), "bootstrap/add", cmdargs...)

	//	resp, err := req.Send(b.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}
	//	defer resp.Close()

	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	out := &BootStrapOutput{}
	//	err = json.NewDecoder(resp.Output).Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}

	//	return out, nil
}

func (b *BootStrapCommand) AddDefault() (*BootStrapOutput, error) {
	cmdargs := []string{}

	out := &BootStrapOutput{}
	err := b.Request("bootstrap/add/default", cmdargs...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	return out, nil

	//	req := b.NewRequest(context.Background(), "bootstrap/add/default", cmdargs...)

	//	resp, err := req.Send(b.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}
	//	defer resp.Close()

	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	out := &BootStrapOutput{}
	//	err = json.NewDecoder(resp.Output).Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}

	//	return out, nil
}

/*
USAGE
  ipfs bootstrap rm [<peer>]... - Remove peers from the bootstrap list.

SYNOPSIS
  ipfs bootstrap rm [--all] [--] [<peer>...]

ARGUMENTS

  [<peer>]... - A peer to add to the bootstrap list (in the format '<multiaddr>/<peerID>')

OPTIONS

  --all bool - Remove all bootstrap peers. (Deprecated, use 'all' subcommand).

DESCRIPTION

  Outputs the list of peers that were removed.

  SECURITY WARNING:

  The bootstrap command manipulates the "bootstrap list", which contains
  the addresses of bootstrap nodes. These are the *trusted peers* from
  which to learn about other peers in the network. Only edit this list
  if you understand the risks of adding or removing nodes from this list.

SUBCOMMANDS
  ipfs bootstrap rm all - Remove all peers from the bootstrap list.

  Use 'ipfs bootstrap rm <subcmd> --help' for more information about each command.
*/

func (b *BootStrapCommand) Rm(peer string) (*BootStrapOutput, error) {
	cmdargs := []string{peer}

	out := &BootStrapOutput{}
	err := b.Request("bootstrap/rm", cmdargs...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	return out, nil

	//	req := b.NewRequest(context.Background(), "bootstrap/rm", cmdargs...)

	//	resp, err := req.Send(b.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}
	//	defer resp.Close()

	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	out := &BootStrapOutput{}
	//	err = json.NewDecoder(resp.Output).Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}

	//	return out, nil
}

func (b *BootStrapCommand) RmAll() (*BootStrapOutput, error) {
	cmdargs := []string{}
	out := &BootStrapOutput{}
	err := b.Request("bootstrap/rm/all", cmdargs...).Exec(context.Background(), out)
	if err != nil {
		return nil, err
	}
	return out, nil
	//	req := b.NewRequest(context.Background(), "bootstrap/rm/all", cmdargs...)

	//	resp, err := req.Send(b.HttpCli)
	//	if err != nil {
	//		return nil, err
	//	}
	//	defer resp.Close()

	//	if resp.Error != nil {
	//		return nil, resp.Error
	//	}

	//	out := &BootStrapOutput{}
	//	err = json.NewDecoder(resp.Output).Decode(out)
	//	if err != nil {
	//		return nil, err
	//	}

	//	return out, nil
}
